package com.dex4d.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GsonUtil {

    public static <T> List<T> jsonToArrayList(String json, Class<T> clazz) {
        ArrayList arrayList = null;

        try {
            Type type = (new TypeToken<ArrayList<JsonObject>>() {
            }).getType();
            List<JsonObject> jsonObjects = (List) (new Gson()).fromJson(json, type);
            arrayList = new ArrayList();
            Iterator var5 = jsonObjects.iterator();

            while (var5.hasNext()) {
                JsonObject jsonObject = (JsonObject) var5.next();
                arrayList.add((new Gson()).fromJson(jsonObject, clazz));
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return arrayList;
    }


    public static <T> List<T> jsonToArrayList(JsonElement json, Class<T> clazz) {
        ArrayList arrayList = null;

        try {
            if (!json.isJsonArray()) {
                return new ArrayList<>();
            }
            Type type = (new TypeToken<ArrayList<JsonObject>>() {
            }).getType();
            List<JsonObject> jsonObjects = (List) (new Gson()).fromJson(json, type);
            arrayList = new ArrayList();
            Iterator var5 = jsonObjects.iterator();

            while (var5.hasNext()) {
                JsonObject jsonObject = (JsonObject) var5.next();

                arrayList.add((new Gson()).fromJson(jsonObject, clazz));

            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return arrayList;
    }


    public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
        Gson gson = new Gson();
        T result = gson.fromJson(jsonData, type);
        return result;
    }

    public static <T> T parseJsonWithGson(JsonElement jsonData, Class<T> type) {
        Gson gson = new Gson();
        if (!jsonData.isJsonObject()) {
            return null;
        }
        T result = gson.fromJson(jsonData, type);
        return result;
    }


    public static String toJsonString(Object clazz) {
        Gson gson = new Gson();
        return gson.toJson(clazz);
    }


    public static String toJson(Object clazz) {

        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation() //不对没有用@Expose注解的属性进行操作
                .enableComplexMapKeySerialization() //当Map的key为复杂对象时,需要开启该方法
                .serializeNulls() //当字段值为空或null时，依然对该字段进行转换
                .setDateFormat("yyyy-MM-dd HH:mm:ss:SSS") //时间转化为特定格式
                .setPrettyPrinting() //对结果进行格式化，增加换行
                .disableHtmlEscaping() //防止特殊字符出现乱码
                .create();
        return gson.toJson(clazz);
    }


}
